package group9;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.*;

/**
 * @author Claudio Herreros, Dylan James, Tyler Berg
 *
 */
@SuppressWarnings("serial")
public class AddUserStoryServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {

		String title = req.getParameter("title");
		String description = req.getParameter("description");
		String testNotes = req.getParameter("testnotes");
		
		User user = (User) req.getSession().getAttribute("User");

		PersistenceManager pm = PMF.get().getPersistenceManager();
		user = (User) pm.getObjectById(User.class, user.getKey());

		if (user == null)
		{
			resp.sendRedirect("index.html");
			return;
		}
		
		UserStory userStory = new UserStory(title, description, testNotes, user);
		List<UserStory> sto = user.getMyUserStories();
		user.addUserStory(userStory);
		pm.close();
		
		try{
			pm = PMF.get().getPersistenceManager();
			pm.makePersistent(userStory);
		}finally{
			pm.close();
		}
		
		req.getSession().setAttribute("User", user);
		
		resp.sendRedirect("/home.jsp");
	}
}
